﻿using System;
using System.Collections.Generic;
using System.Xml;
using Autodesk.Revit.DB;
using Transfer.CommonUnit;

namespace Transfer.SingleFloor
{
	public class SelectionSet
	{
		public string SetName
		{
			get
			{
				return this.m_SetName;
			}
			set
			{
				this.m_SetName = value;
			}
		}

		public List<ElementId> SelectedElementIdList
		{
			get
			{
				return this.m_SelectionSet;
			}
			set
			{
				this.m_SelectionSet = value;
			}
		}

		public List<SelectionSet.TypeSelectedState> TypeSelectedStateList
		{
			get
			{
				return this.m_TypeSelectedState;
			}
			set
			{
				this.m_TypeSelectedState = value;
			}
		}

		public SelectionSet()
		{
		}

		public SelectionSet(SelectionSet other)
		{
			this.m_SetName = other.m_SetName;
			this.m_SelectionSet = other.m_SelectionSet;
			this.m_TypeSelectedState = other.m_TypeSelectedState;
		}

		public SelectionSet(string setName, List<ElementId> elementsIdList, List<ComponentType> theTypeList)
		{
			this.m_SetName = setName;
			this.m_SelectionSet = elementsIdList;
			foreach (ComponentType theType in theTypeList)
			{
				SelectionSet.TypeSelectedState typeSelectedState = new SelectionSet.TypeSelectedState();
				typeSelectedState.TheType = theType;
				typeSelectedState.TheChecked = true;
				this.m_TypeSelectedState.Add(typeSelectedState);
			}
		}

		public void ReadInfoFromFile(Autodesk.Revit.DB.Document doc, XmlElement setNode)
		{
			this.m_SelectionSet.Clear();
			this.m_TypeSelectedState.Clear();
			this.m_SetName = setNode.GetAttribute("Name");
			XmlNode xmlNode = setNode.SelectSingleNode("IdSet") as XmlElement;
			List<string> list = new List<string>();
			foreach (object obj in xmlNode.ChildNodes)
			{
				ElementId elementId = new ElementId(Convert.ToInt32((((XmlNode)obj) as XmlElement).GetAttribute("Id")));
				if (this.HasElement(doc, elementId, ref list))
				{
					this.m_SelectionSet.Add(elementId);
				}
			}
			foreach (object obj2 in (setNode.SelectSingleNode("TypeSelectStateList") as XmlElement).ChildNodes)
			{
				XmlNode xmlNode2 = (XmlNode)obj2;
				XmlElement xmlElement = xmlNode2 as XmlElement;
				SelectionSet.TypeSelectedState typeSelectedState = new SelectionSet.TypeSelectedState();
				string attribute = xmlElement.GetAttribute("TypeName");
				string categoryName = xmlElement.GetAttribute("CategeryName");
				if (list.FindIndex((string s) => s.CompareTo(categoryName) == 0) != -1)
				{
					typeSelectedState.TheType = new ComponentType(attribute, categoryName);
					typeSelectedState.TheChecked = Convert.ToBoolean(xmlElement.GetAttribute("Checked"));
					this.m_TypeSelectedState.Add(typeSelectedState);
				}
			}
		}

		public bool HasElement(Autodesk.Revit.DB.Document doc, ElementId id, ref List<string> categoryNameList)
		{
			bool result;
			try
			{
				Element element = doc.GetElement(id);
				if (element != null)
				{
					string categoryName = element.Category.Name;
					if (categoryNameList.FindIndex((string s) => s.CompareTo(categoryName) == 0) == -1)
					{
						categoryNameList.Add(categoryName);
					}
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public void WriteInfoToFile(XmlDocument xmlDoc, ref XmlElement setNode)
		{
			setNode.SetAttribute("Name", this.m_SetName);
			XmlElement xmlElement = xmlDoc.CreateElement("", "IdSet", "");
			setNode.AppendChild(xmlElement);
			foreach (ElementId elementId in this.m_SelectionSet)
			{
				XmlElement xmlElement2 = xmlDoc.CreateElement("", "ElementId", "");
				xmlElement2.SetAttribute("Id", elementId.IntegerValue.ToString());
				xmlElement.AppendChild(xmlElement2);
			}
			XmlElement xmlElement3 = xmlDoc.CreateElement("", "TypeSelectStateList", "");
			setNode.AppendChild(xmlElement3);
			foreach (SelectionSet.TypeSelectedState typeSelectedState in this.m_TypeSelectedState)
			{
				XmlElement xmlElement4 = xmlDoc.CreateElement("", "TypeState", "");
				xmlElement4.SetAttribute("TypeName", typeSelectedState.TheType.m_Type);
				xmlElement4.SetAttribute("CategeryName", typeSelectedState.TheType.m_Category);
				xmlElement4.SetAttribute("Checked", typeSelectedState.TheChecked.ToString());
				xmlElement3.AppendChild(xmlElement4);
			}
		}

		private string m_SetName = "";

		private List<ElementId> m_SelectionSet = new List<ElementId>();

		private List<SelectionSet.TypeSelectedState> m_TypeSelectedState = new List<SelectionSet.TypeSelectedState>();

		public class TypeSelectedState
		{
			public ComponentType TheType
			{
				get
				{
					return this.m_Type;
				}
				set
				{
					this.m_Type = value;
				}
			}

			public bool TheChecked
			{
				get
				{
					return this.m_Checked;
				}
				set
				{
					this.m_Checked = value;
				}
			}

			public TypeSelectedState()
			{
			}

			public TypeSelectedState(SelectionSet.TypeSelectedState other)
			{
				this.m_Type = other.m_Type;
				this.m_Checked = other.m_Checked;
			}

			public TypeSelectedState(ComponentType theType, bool theChecked)
			{
				this.m_Type = theType;
				this.m_Checked = theChecked;
			}

			private ComponentType m_Type;

			private bool m_Checked;
		}
	}
}
